app.controller('NewProductModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var product_name, product_model,product_describe,product_factoryname;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    product_name = $scope.oo.name;                   
                    product_model = $scope.oo.model;
                    product_describe = $scope.oo.describe;
                    product_factoryname = $scope.oo.factory_name;                                   
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newproductModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-product/create",
                        {
                            'name': product_name,                        
                            'model': product_model ,
                            'describe': product_describe,
                            'factory_name' : product_factoryname, 
                            'token':$localStorage.token             

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=='创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/product');                      

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        $scope.authError = data.error.code;
                        if ($scope.authError == 401) {
                            $location.path('access.signin');
                        }
                    })
                });
            };
        }])

app.controller('EditProductModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var product_name, product_pid, product_secret,product_model,product_describe,product_factoryname;
            var productname,productpid, productsecret,productmodel,productdescribe,productfactoryname,productid;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.name = product_name;
                $scope.oo.pid = product_pid;
                $scope.oo.product_secret = product_secret;
                $scope.oo.model = product_model;
                $scope.oo.factory_name = product_factoryname;
                $scope.oo.describe = product_describe;                

                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    productname = $scope.oo.name;
                    productpid = $scope.oo.pid;
                    productsecret = $scope.oo.product_secret;
                    productmodel = $scope.oo.model;
                    productdescribe = $scope.oo.describe;
                    productfactoryname = $scope.oo.factory_name;   
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };

            $scope.editproduct = function(obj){
                //console.log(obj);
                product_name = obj.name;
                product_pid = obj.pid;
                product_secret = obj.product_secret;
                product_model = obj.model;
                product_describe = obj.describe;
                product_factoryname = obj.factory_name;
                productid = obj.product_id;           
                $scope.open();

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'editproductModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"tbl-product/update",
                        {
                            'product_id': productid,
                            'name': productname,                             
                            'model': productmodel ,
                            'describe': productdescribe,
                            'factory_name' : productfactoryname, 
                            'token':$localStorage.token                         

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="产品修改成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/product');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])


app.controller('DelProductModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var product_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.delproduct = function (aid) {
                //console.log(aid);
                product_id = aid;             
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delproductModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"tbl-product/delete",
                        {
                           
                            'product_id': product_id,                             
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/product');

                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })


                });
            };
        }])